package sk.tuke.client.view;

import sk.tuke.client.ApplicationClickHandler;
import sk.tuke.client.interfaces.LayoutFactory;

import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class PatientsListView extends VLayout {
	
	public PatientsListView() {
		super();

		RestDataSource datasource = new RestDataSource();  
        datasource.setDataURL("http://localhost:8080/healthCard/usermanagement/getpatients");  
        datasource.setRecordXPath("GetPatientsOutput/patients");  
        datasource.setCallbackParam("GetPatientsOutput");
        datasource.setDataFormat(DSDataFormat.JSON);

        DataSourceField number = new DataSourceField("number", FieldType.INTEGER, "Number");  
        DataSourceField firstname = new DataSourceField("firstname", FieldType.TEXT, "Firstname");  
        DataSourceField lastname = new DataSourceField("lastname", FieldType.TEXT, "Lastname");  
        DataSourceField street = new DataSourceField("street", FieldType.TEXT);  
        DataSourceField streetNum = new DataSourceField("streetNum", FieldType.TEXT);
        DataSourceField city = new DataSourceField("city", FieldType.TEXT);
        datasource.setFields(number, firstname, lastname, street, streetNum, city);  
        
        final ListGrid patientGrid = new ListGrid() {  
            @Override  
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {  
  
                String fieldName = this.getFieldName(colNum);  
  
                if (fieldName.equals("menuField")) {  
                    HLayout recordCanvas = new HLayout(3);  
                    recordCanvas.setHeight(22);    
                 
                    ImgButton openImg = new ImgButton();
                    openImg.setShowDown(false);  
                    openImg.setShowRollOver(false);   
                    openImg.setSrc("open.png");  
                    openImg.setPrompt("Open Profile");  
                    openImg.setHeight(16);  
                    openImg.setWidth(16);
                    openImg.addClickHandler(new ApplicationClickHandler(){
            			@Override
            			public void onClick(ClickEvent event) {
            				PatientProfileView.Factory factory = new PatientProfileView.Factory();
            				Canvas view = factory.create(record.getAttributeAsInt("number"));
            				southLayout.setMembers(view, westLayout);
            			}
                    });
    
                    ImgButton editImg = new ImgButton();
                    editImg.setShowDown(false);  
                    editImg.setShowRollOver(false);  
                    editImg.setSrc("edit.png");  
                    editImg.setPrompt("Edit Profile");  
                    editImg.setHeight(16);  
                    editImg.setWidth(16); 
                    editImg.addClickHandler(new ApplicationClickHandler(){
            			@Override
            			public void onClick(ClickEvent event) {
            				SC.say("Edit profile");
            			}
                    });
                    
                    recordCanvas.addMember(openImg);  
                    recordCanvas.addMember(editImg);
                    return recordCanvas;  
                } else {  
                    return null;  
                }  
  
            }  
        };
        patientGrid.setShowRecordComponents(true);          
        patientGrid.setShowRecordComponentsByCell(true);   
        patientGrid.setShowFilterEditor(true);  
        patientGrid.setFilterOnKeypress(true);
        patientGrid.setHeight(480);
        patientGrid.setMargin(40);
        patientGrid.setDataSource(datasource);    
        patientGrid.setAutoFetchData(true);
        patientGrid.setShowAllRecords(true);
        patientGrid.fetchData();
        
        ListGridField numberField = new ListGridField("number", 60);
        ListGridField firstnameField = new ListGridField("firstname");
        ListGridField lastnameField = new ListGridField("lastname");
        ListGridField streetField = new ListGridField("street", "Street");
        streetField.setDataPath("address/street");
        ListGridField streetNumField = new ListGridField("streetNum", " ", 30);
        streetNumField.setDataPath("address/streetNum");
        ListGridField cityField = new ListGridField("city", "City");
        cityField.setDataPath("address/city");
        ListGridField menuField = new ListGridField("menuField", "Menu", 60);
        menuField.setCanFilter(false);
        patientGrid.setFields(numberField, firstnameField, lastnameField, streetField, streetNumField, cityField, menuField);
        
   		// initialise the Accounts View layout container
		this.addStyleName("crm-ContextArea");
		this.setWidth("*");

		// add the List Grid to the Accounts View layout container
		HLayout a = new HLayout();
		a.addMember(patientGrid);
		this.addMember(a);
		
	}

	public static class Factory implements LayoutFactory {

		public Canvas create() {
			return new PatientsListView();
		}

	}
}
